package club.zhcs.capture.biz.dept.impl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import com.dgj.utils.collection.Lists;
import com.dgj.utils.db.SqlActuator;
import com.dgj.utils.entries.Result;
import com.dgj.utils.web.pager.Pager;

import club.zhcs.capture.bean.dept.Area;
import club.zhcs.capture.bean.dept.City;
import club.zhcs.capture.bean.dept.Province;
import club.zhcs.capture.bean.dept.Subsidiary;
import club.zhcs.capture.biz.dept.ISubsidiaryService;
import club.zhcs.capture.dao.dept.IAreaDao;
import club.zhcs.capture.dao.dept.ICityDao;
import club.zhcs.capture.dao.dept.IProvinceDao;
import club.zhcs.capture.dao.dept.ISubsidiaryDao;
import static club.zhcs.capture.vo.Consts.SystemValues.*;

@IocBean
public class SubsidiaryService implements ISubsidiaryService {
	@Inject
	ISubsidiaryDao subsidiaryDao;

	@Inject
	IProvinceDao provinceDao;

	@Inject
	ICityDao cityDao;

	@Inject
	IAreaDao areaDao;

	@Inject
	PropertiesProxy sqls;

	@Override
	public List<Province> listProvince() {
		return provinceDao.findAll();
	}

	@Override
	public List<City> listCity(String code) {
		if (Strings.equalsIgnoreCase("0", code)) {
			return Lists.newArrayList();
		}

		if (Strings.isBlank(code)) {
			return cityDao.findAll();
		}
		return cityDao.search(Cnd.where("provinceCode", "=", code));
	}

	@Override
	public List<Area> listArea(String code) {
		if (Strings.equalsIgnoreCase("0", code)) {
			return Lists.newArrayList();
		}

		if (Strings.isBlank(code)) {
			return areaDao.findAll();
		}
		return areaDao.search(Cnd.where("cityCode", "=", code));
	}

	@Override
	public Result add(Subsidiary subsidiary) {
		return subsidiaryDao.save(subsidiary) == null ? Result.fail("添加分支机构失败!") : Result.success().addData("subsidiary", subsidiary);
	}

	@Override
	public Pager<Subsidiary> listByPage(int page) {
		Pager<Subsidiary> pager = new Pager<Subsidiary>(PAGESIZE, page);
		pager.setEntities(subsidiaryDao.searchByPage(null, page, PAGESIZE));
		pager.setCount(subsidiaryDao.countAll());
		return pager;
	}

	@Override
	public boolean delete(int id) {
		return subsidiaryDao.delById(id);
	}

	@Override
	public Pager<Subsidiary> search(int page, String key) {
		String searchKey = String.format("%%%s%%", key);
		Condition cnd = Cnd.where("name", "like", searchKey).or("code", "like", searchKey);
		Pager<Subsidiary> pager = new Pager<Subsidiary>(PAGESIZE, page);
		pager.setEntities(subsidiaryDao.searchByPage(cnd, page, PAGESIZE));
		pager.setCount(subsidiaryDao.countByCnd(cnd));
		return pager;
	}

	@Override
	public Record getAllInfo(int id) {
		Sql sql = Sqls.create(sqls.get("find.all.subsidiary.info.by.id"));
		sql.params().set("id", id);
		List<Record> records = SqlActuator.runReport(sql, subsidiaryDao.getDao());
		return records == null ? null : records.get(0);
	}

	@Override
	public Result update(Subsidiary subsidiary) {
		return !subsidiaryDao.update(subsidiary) ? Result.fail("更新分支机构失败!") : Result.success().addData("subsidiary", subsidiary);
	}

	@Override
	public List<Subsidiary> listAll() {
		return subsidiaryDao.findAll();
	}
}
